package com.tyjt.vehfinder.core.akka;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.OneForOneStrategy;
import akka.actor.SupervisorStrategy;
import akka.japi.pf.DeciderBuilder;
import lombok.extern.slf4j.Slf4j;
import scala.concurrent.duration.Duration;

import static akka.actor.SupervisorStrategy.restart;

/**
 * @author 种鑫
 * @date 2024/7/23 10:18
 */
@Slf4j
public class Supervisor extends AbstractActor {

    private static final SupervisorStrategy strategy =
            new OneForOneStrategy(
                    10,
                    Duration.create("1 minute"),
                    DeciderBuilder
                            .match(RuntimeException.class, e -> restart())
                            .matchAny(o -> SupervisorStrategy.escalate())
                            .build());

    @Override
    public SupervisorStrategy supervisorStrategy() {
        return strategy;
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(CreateActorMsg.class, p -> {
                    ActorRef childRef = getContext().actorOf(p.getProps(), p.getVehId());
                    getSender().tell(childRef, getSelf());
                })
                .build();
    }


}
